home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / T_BCINFO.ARJ / TI726.ASC < prev    next >
Text File  |  1992-02-25  |  6KB  |  199 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Borland C++                            NUMBER  :  726
  9.   VERSION  :  2.0
  10.        OS  :  DOS
  11.      DATE  :  February 25, 1992                        PAGE  :  1/3
  12.  
  13.     TITLE  :  "Null Pointer Assignment" Errors Explained
  14.  
  15.  
  16.  
  17.  
  18.   Null Pointer Assignment
  19.  
  20.   1. What is a Null Pointer Assignment error?
  21.  
  22.   The Null Pointer Assignment error is generated in programs that
  23.   corrupt the bottom of the data segment in such a fashion as to
  24.   indicate a high probability that an improperly-initialized
  25.   pointer has been used.  The error can only be generated in the
  26.   small and medium memory models.
  27.  
  28.   2. What causes a Null Pointer Assignment error?
  29.  
  30.   Borland places four zero bytes at the bottom of the data segment,
  31.   followed by the Borland copyright notice.  In the small and
  32.   medium memory models, a null pointer points to DS:0000.  Thus
  33.   assigning a value to the memory referenced by this pointer will
  34.   overwrite the first zero byte in the data segment.  At program
  35.   termination, the four zeros and the copyright banner are checked.
  36.   If either has been modified, then the Null Pointer Assignment
  37.   error is generated.  Note that the pointer may not truly be null,
  38.   but may be a wild pointer that references these key areas in the
  39.   data segment.
  40.  
  41.   3. How can I debug a Null Pointer Assignment error?
  42.  
  43.   In either the Integrated Development Environment (except BCX) or
  44.   in Turbo Debugger, set two watches on these key memory locations.
  45.   These watches, and what they should display in the watch window,
  46.   are:
  47.  
  48.       *(char*)0,4m    "Borland C++ - Copyright 1991 Borland Intl."
  49.       (char *)0       00 00 00 00
  50.  
  51.   Of course, the copyright banner will vary depending on your
  52.   version of the Borland C/C++ compiler.
  53.  
  54.   Caution:  The first of these watches--*(char *)0,4m--cannot be
  55.   used in BCX or a general protection fault will be generated;
  56.   however, the second watch--(char *)0--will perform properly.
  57.  
  58.   Step through your program and monitor theses values in the watch
  59.   window.  At the point where one of them changes, you have just
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Borland C++                            NUMBER  :  726
  75.   VERSION  :  2.0
  76.        OS  :  DOS
  77.      DATE  :  February 25, 1992                        PAGE  :  2/3
  78.  
  79.     TITLE  :  "Null Pointer Assignment" Errors Explained
  80.  
  81.  
  82.  
  83.  
  84.   executed a statement that uses a pointer that has not been
  85.   properly initialized.
  86.  
  87.   The most common cause of this error is probably declaring a
  88.   pointer and then using it before allocating memory for it.  For
  89.   example, compile this program in the small memory model and
  90.   execute it:
  91.  
  92.   #include <dos.h>
  93.   #include <stdio.h>
  94.   #include <string.h>
  95.  
  96.   int main(void)
  97.   {
  98.       char *ptr, *banner;
  99.       banner = (char *) MK_FP(_DS, 4);
  100.       printf("banner: %s\n", banner);
  101.       strcpy(ptr, "Where will this text be copied?!?");
  102.       printf("&ptr = %Fp\n", (void far*) &ptr[0]);
  103.       printf("banner: %s\n", banner); return 0;
  104.   }
  105.  
  106.   One of the best debugging techniques is to turn on all warning
  107.   compiler messages.  If the above program is compiled with
  108.   warnings turned off, no warning messages will be generated.
  109.   However, if all warnings are turned on, both the strcpy() and
  110.   printf() calls using the buffer variable will generate warnings.
  111.   Be particularly suspicious of any warnings that a variable might
  112.   be used before being initialized, or of a suspicious pointer
  113.   assignment.  With the command-line compiler, just include the -w
  114.   switch. In the IDE, select the appropriate menu option for
  115.   compiler messages and work through all the menu windows, ensuring
  116.   all items are checked.  In Turbo C++ and Borland C++, some of the
  117.   option windows have sub-menus indicated by a 'more' selection box
  118.   in the lower left corner of the menu box.
  119.  
  120.   4. Why is a Null Pointer Assignment error not generated in all
  121.      models?
  122.  
  123.   In the compact, large and huge memory models, far pointers are
  124.   used for data.  Therefore, a null pointer will reference
  125.   0000:0000, or the base of system memory, and using it will not
  126.   cause a corruption of the key values at the base of the data
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.   PRODUCT  :  Borland C++                            NUMBER  :  726
  141.   VERSION  :  2.0
  142.        OS  :  DOS
  143.      DATE  :  February 25, 1992                        PAGE  :  3/3
  144.  
  145.     TITLE  :  "Null Pointer Assignment" Errors Explained
  146.  
  147.  
  148.  
  149.  
  150.   segment.  Modifying the base of system memory usually causes a
  151.   system crash, however.  Although it would be possible that a wild
  152.   pointer would overwrite the key values, it would not indicate a
  153.   null pointer.
  154.  
  155.   In the tiny memory model, DS = CS = SS.  Therefore, using a null
  156.   pointer will overwrite the beginning of the code segment.
  157.  
  158.   5.  Can anything else generate a Null Pointer Assignment error?
  159.  
  160.   Using a wild pointer that happens to reference the base area of
  161.   the data segment--thus causing a corruption of the zeros or the
  162.   copyright banner--will generate this error.  Since data
  163.   corruption or stack corruption could cause an otherwise-valid
  164.   pointer to be corrupted and point to the base of the data
  165.   segment, any memory corruption could result in this error being
  166.   generated.  If the pointer used in the program statement which
  167.   corrupts the key values appears to have been properly
  168.   initialized, place a watch on that pointer.  Step through your
  169.   program again and watch for its value (address) to change.
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.